package com.niiwoo.civet.trade.listener;

import java.io.IOException;
import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.constant.LoanFlowMqConstant;
import com.niiwoo.civet.trade.service.local.loan.creditaduit.CreditAuditCompositeService;
import com.rabbitmq.client.Channel;

/**
 * 借款信审MQ Listner
 * 
 * @author zhoulinquan
 */
@Component
public class LoanCreditAuditListener {

	private static final Logger logger = LoggerFactory.getLogger(LoanCreditAuditListener.class);
	
	@Autowired
	private CreditAuditCompositeService creditAuditCompositeService;

    @RabbitListener(queues = {LoanFlowMqConstant.Queue.LOAN_SYSTEM_AUDIT_QUEUE})
    public void creditAudit(Message message, Channel channel) throws IOException {
    	try {
    		String reqMsg = new String(message.getBody(), Charset.forName("UTF-8"));
    		Long flowExecuteId = JSON.parseObject(reqMsg).getLong("flowExecuteId");
    		
    		creditAuditCompositeService.callTianchengCreditAuditService(flowExecuteId);
    	} catch (Exception e) {
    		logger.error("借款流程数据异常,请求参数={},异常信息={}", message.getBody(), e);
    	} finally  {
    		try {
                //确认消息已经接收;
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException ie) {
            	logger.error("信审MQ消息确认失败,异常信息={}", ie);
            }
    	}
    }
}
